# ========================================================== # Device-Wide Chrome Cleanup Script # ========================================================== # Use Case: # Cleans Chrome cache data for ALL users on the system # # Default Cleanup: # - Cache # - Code Cache # - GPUCache # # Optional Cleanup (Opt-In): # - Cookies # - History # - Sessions # # Arguments: # ExcludedUsers=user1,user2 # ExplicitTargets=Cookies,History,Sessions # # Examples: # .\ChromeCleanup.ps1 # # .\ChromeCleanup.ps1 ` # ExcludedUsers=Admin,TestUser ` # ExplicitTargets=Cookies,History # # WARNING: # ExplicitTargets may: # - Log users out from websites # - Remove browsing history # - Remove open sessions/tabs # ========================================================== param( [string[]]$ExcludedUsers = @( "Public", "Default", "Default User", "All Users" ), [string[]]$ExplicitTargets = @() ) # ---------------------------------------------------------- # Create Event Log Source # ---------------------------------------------------------- $EventSource = "ChromeCleanupScript" try { if (-not [System.Diagnostics.EventLog]::SourceExists($EventSource)) { New-EventLog ` -LogName Application ` -Source $EventSource } } catch { Write-Host "[WARNING] Could not create Windows Event Log source." } # ---------------------------------------------------------- # Stop Chrome Processes # ---------------------------------------------------------- Write-Host "`n[INFO] Closing Google Chrome..." Get-Process chrome -ErrorAction SilentlyContinue | Stop-Process -Force Start-Sleep -Seconds 3 # ---------------------------------------------------------- # Get All User Profiles # ---------------------------------------------------------- $UserProfiles = Get-ChildItem "C:\Users" -Directory | Where-Object { $_.Name -notin $ExcludedUsers } if ($UserProfiles.Count -eq 0) { Write-Host "[ERROR] No user profiles found." exit } # ---------------------------------------------------------- # Process Each User # ---------------------------------------------------------- foreach ($User in $UserProfiles) { $ChromePath = "$($User.FullName)\AppData\Local\Google\Chrome\User Data\Default" if (!(Test-Path $ChromePath)) { Write-Host "`n[INFO] Chrome profile not found for user: $($User.Name)" continue } Write-Host "`n=========================================" Write-Host " Processing User: $($User.Name)" Write-Host "=========================================" # ------------------------------------------------------ # Backup Location # ------------------------------------------------------ $BackupRoot = "C:\ChromeCleanupBackup\$($User.Name)" if (!(Test-Path $BackupRoot)) { New-Item ` -Path $BackupRoot ` -ItemType Directory ` -Force | Out-Null } # ------------------------------------------------------ # Default Targets # ------------------------------------------------------ $Targets = @( "$ChromePath\Cache", "$ChromePath\Code Cache", "$ChromePath\GPUCache" ) # ------------------------------------------------------ # Optional Explicit Targets # ------------------------------------------------------ foreach ($TargetName in $ExplicitTargets) { switch ($TargetName.Trim().ToLower()) { "cookies" { $Targets += "$ChromePath\Cookies" } "history" { $Targets += "$ChromePath\History" $Targets += "$ChromePath\Visited Links" $Targets += "$ChromePath\Web Data" } "sessions" { $Targets += "$ChromePath\Sessions" } default { throw "[ERROR] Unsupported ExplicitTarget: $TargetName" } } } # ------------------------------------------------------ # Cleanup # ------------------------------------------------------ foreach ($Target in $Targets) { if (Test-Path $Target) { try { # -------------------------------------------------- # Backup Before Deletion # -------------------------------------------------- $BackupPath = Join-Path ` $BackupRoot ` (Split-Path $Target -Leaf) Copy-Item ` -Path $Target ` -Destination $BackupPath ` -Recurse ` -Force ` -ErrorAction Stop Write-Host "[BACKUP] Backup created: $BackupPath" $Item = Get-Item ` $Target ` -ErrorAction Stop # -------------------------------------------------- # Block Junctions / Symbolic Links # -------------------------------------------------- if ($Item.Attributes -band [System.IO.FileAttributes]::ReparsePoint) { throw "[ERROR] Reparse point detected: $Target" } # -------------------------------------------------- # Clear Folder Contents # -------------------------------------------------- if ($Item -is [System.IO.DirectoryInfo]) { Get-ChildItem ` -Path $Target ` -Force ` -ErrorAction Stop | Where-Object { -not ($_.Attributes -band [System.IO.FileAttributes]::ReparsePoint) } | Remove-Item ` -Recurse ` -Force ` -ErrorAction Stop } else { Remove-Item ` $Target ` -Force ` -ErrorAction Stop } Write-Host "[SUCCESS] Cleared: $Target" # -------------------------------------------------- # Windows Event Log # -------------------------------------------------- try { Write-EventLog ` -LogName Application ` -Source $EventSource ` -EntryType Information ` -EventId 55001 ` -Message "Deleted Chrome data: $Target for user $($User.Name)" } catch { Write-Host "[WARNING] Failed to write Event Log." } } catch { Write-Host "[WARNING] Could not clear: $Target" try { Write-EventLog ` -LogName Application ` -Source $EventSource ` -EntryType Warning ` -EventId 55002 ` -Message "Failed to delete Chrome data: $Target for user $($User.Name)" } catch { Write-Host "[WARNING] Failed to write failure Event Log." } } }else{ Write-Host "[INFO] Target not found, skipping: $Target" } } } # ---------------------------------------------------------- # Completion # ---------------------------------------------------------- Write-Host "`n=========================================" Write-Host " Device-Wide Chrome Cleanup Completed" Write-Host "=========================================" # ---------------------------------------------------------- # Optional: Restart Chrome # ---------------------------------------------------------- try { Start-Process "chrome.exe" Write-Host "[INFO] Chrome restarted." } catch { Write-Host "[ERROR] Chrome could not be started." }